package com.example.lending.View;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toolbar;

import com.example.lending.Model.Book;
import com.example.lending.Model.Borrow;
import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.View.Adapter.BookAdapter;
import com.example.lending.ViewModel.BookViewModel;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * A simple {@link Fragment} subclass.
 */
public class AllBookFragment extends Fragment {


    public AllBookFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_all_book, container, false);
    }
    RecyclerView recyclerView;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        BookViewModel bookViewModel;
        recyclerView=getView().findViewById(R.id.rv_listAllBook);
        bookViewModel= ViewModelProviders.of(getActivity()).get(BookViewModel.class);
        //bookAdapter=new BookAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        final BookAdapter bookAdapter=new BookAdapter(bookViewModel.getAllBooksLive().getValue(),getActivity());
        recyclerView.setAdapter(bookAdapter);
        bookAdapter.setAllBooks(bookViewModel.getAllBooksLive().getValue());//这句话可能有问题
        bookViewModel.getAllBooksLive().observe(getActivity(), new Observer<List<Book>>() {
            @Override
            public void onChanged(List<Book> books) {//问题在于这里是change了才放数据，不改变放不放呀？
                bookAdapter.setAllBooks(books);
                bookAdapter.notifyDataSetChanged();;
            }
        });
    }


}
